#ifndef CONFIG_H
#define CONFIG_H

#include <boost/property_tree/info_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include "boost/property_tree/xml_parser.hpp"
#include "boost/property_tree/json_parser.hpp"
#include <boost/property_tree/ini_parser.hpp>
#include <boost/system/system_error.hpp>

#include <iostream>

namespace Fast {

enum FILE_FORMAT{
    T_JSON = 0,
    T_INI ,
    T_XML,
    T_INFO,
    T_ALL
};

using namespace boost::property_tree;



class config
{
public:
    config(const std::string &fn);

    void getUsage();
    void sync();

    /// fen li format
    std::string SplitFilename (const std::string& str)
    {
        std::string::size_type pos=str.rfind('.');
        std::string ext=str.substr(pos==std::string::npos?str.length():pos+1);
        return ext;
    }


    template<typename T>
    T get(const std::string &index) const{
        T ret ;
        try {
            ret = pt.get<T>(index);
        } catch (const boost::exception &e) {
            std::cout<<"Error Read!"<<std::endl;

        }catch(const boost::system::system_error &e){
            std::cout <<"system:"<< e.what() << std::endl ;
        }
        return ret;
    }

    template<typename T>
    bool put(const std::string &index,T value) const{
        bool ret = true;
        try {
            pt.put<T>(index,value);
        } catch (const boost::exception &e) {
            ret = false;
            std::cout<<"Error Read!"<<std::endl;

        }catch(const boost::system::system_error &e){
            std::cout <<"system:"<< e.what() << std::endl ;
            ret = false;
        }

        return ret;
    }


private:
    void write2File();

private:
    std::string filename;
    boost::property_tree::ptree pt;
    int ext_format;
};



}



#endif // CONFIG_H
